package org.pp.config;

import cn.hutool.core.io.IoUtil;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.BufferedReader;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

@Configuration
public class BeanConfig {

    @Autowired
    MerchantConfig merchantConfig;

    @Bean
    public RSAAutoCertificateConfig getWxPayConfig() {
        InputStream resourceAsStream =
                MerchantConfig.class.getClassLoader().getResourceAsStream("cert/apiclient_key.pem");
        BufferedReader reader = IoUtil.getReader(resourceAsStream, StandardCharsets.UTF_8);
        String pkey = IoUtil.read(reader);
        return new RSAAutoCertificateConfig.Builder()
                .merchantId(merchantConfig.getMerchantId())
                .privateKey(pkey)
                .merchantSerialNumber(merchantConfig.getMerchantSerialNumber())
                .apiV3Key(merchantConfig.getApiV3Key())
                .build();
    }
}
